<?php
/**
 * Created by PhpStorm.
 * User: function
 * Date: 2018-04-22
 * Time: 17:43
 */

namespace app\seller\controller;
use app\common\logic\AdLogic;
use app\common\logic\AdPlaceLogic;
use app\common\controller\Seller;
use think\Request;
class Ad extends Seller
{
    public function index(){
        
        $logic = new AdLogic();
        $params = paramForm();
        if(Request::instance()->isAjax()){
            $params['user_id'] = SID;
            $logic->setSql($params);
            $list = $logic->prepareDataProvider();
            success($list);
        }else{
            return $this->fetch();
        }
    }
    
    
    public function place(){
        
        $logic = new AdPlaceLogic();
        $params = paramForm();
        if(Request::instance()->isAjax()){
            $logic->setSql($params);
            $list = $logic->prepareDataProvider();
            success($list);
        }else{
            return $this->fetch();
        }
    }
    
    public function buy($pid){
        
        $logic = new AdPlaceLogic();
        $place = $logic->findModel($pid);
        $this->assign('place',$place);
        return $this->fetch();
        
    }
    
    public function my(){
        return $this->fetch();
    }
    
    public function doBuy(){
        $params = paramForm();
        $logic = new AdLogic();
        $params['endtime'] = strtotime($params['totime'])+(int)$params['month']*30*24*3600;
        $params['user_id'] = SID;
        //存好图片
        $thumb = request()->file('image_src');
        if($thumb){
            $params['image_src']=$this->uploadToAlyun($thumb);
        }else{
            error('图片必传');
        }
        
        $rt = $logic->add($params);
        
        if($rt){
            $this->success('申请提交成功,等待管理员审核','/ad');
        }else{
            $this->error('提交失败,可以直接联系我们');
        }
        
    }
    
}